package com.snail.billing.fragment;

import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.os.Page;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.AccountManager;
import com.snail.billing.session.SdoRegisterSession;
import com.snail.billing.util.BillingUtil;

public class RegisterFragment extends Page implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputAccount;
	private EditText inputPwd1;
	private EditText inputPwd2;
	private View button;

	private SdoRegisterSession sdoRegisterSession;
	private String account;
	private String pwd;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_register_activity);
	}

	@Override
	public void onCreate() {
		super.onCreate();
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_register_title));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputAccount = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_account));
		inputPwd1 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_pwd1));
		inputPwd2 = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_input_pwd2));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_register_button));
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(button)) {
			String account = inputAccount.getText().toString();
			if (TextUtils.isEmpty(account)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_account));
				return;
			}
			String pwd = inputPwd1.getText().toString();
			if (TextUtils.isEmpty(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_pwd1));
				return;
			}
			String pwd2 = inputPwd2.getText().toString();
			if (TextUtils.isEmpty(pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_input_pwd2));
				return;
			}
			if (!TextUtils.equals(pwd, pwd2)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_pwd_not_same));
				return;
			}
			if (!BillingUtil.validatePwd(pwd)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_register_pwd_validate));
				return;
			}
			requestSdoRegister(account, pwd);
		}
	}

	private void requestSdoRegister(String account, String pwd) {
		this.account = account;
		this.pwd = pwd;
		HttpApp httpApp = new HttpApp(getContext());
		sdoRegisterSession = new SdoRegisterSession(account, pwd);
		httpApp.setOnHttpResultListener(this);
		httpApp.request(sdoRegisterSession);
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(sdoRegisterSession)) {
				String reponse = (String) sdoRegisterSession.getResponseData();
				try {
					SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
					RegisterXmlHander xmlHandler = new RegisterXmlHander();
					saxParser.parse(new InputSource(new StringReader(reponse)), xmlHandler);
					if ("1".equals(xmlHandler.getCode())) {
						Toast.makeText(getContext(), xmlHandler.getMessage(), Toast.LENGTH_SHORT).show();
						AccountManager.saveAccount(account, pwd);
						getPageManager().finish();
						if (DataCache.getInstance().importParams.loginCallback != null) {
							DataCache.getInstance().importParams.loginCallback.onLoginCallback(account, pwd);
						}
					} else {
						Toast.makeText(getContext(), xmlHandler.getMessage(), Toast.LENGTH_SHORT).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class RegisterXmlHander extends DefaultHandler {

		private String code;

		private String message;

		public String getCode() {
			return code;
		}

		public String getMessage() {
			return message;
		}

		@Override
		public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
			if (localName.equals("RESULT")) {
				for (int i = 0; i < attributes.getLength(); i++) {
					String name = attributes.getLocalName(i);
					String value = attributes.getValue(i);
					if (name.equals("msgcode")) {
						code = value;
					} else if (name.equals("message")) {
						message = value;
					}
				}
			}
		}

	}

}
